package wellness.activities;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.crypto.spec.PSource;

import wellness.activities.beans.LoadBeans;
import wellness.activities.beans.UsuarioBean;
import wellness.activities.data.UsuarioData;
import wellness.activities.util.Dialogs;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Toast;

public class First extends Activities {

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		if (LoadBeans.getUsuario() != null) {
			Intent myIntent = new Intent(First.this, Main.class);
			startActivityForResult(myIntent, 0);
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		UsuarioData data = new UsuarioData(this);
//		data.resetar();
		data.selectAll();
		
		if (LoadBeans.getUsuario() != null) {
			Intent myIntent = new Intent(First.this, Main.class);
			startActivityForResult(myIntent, 0);
		}

		super.onCreate(savedInstanceState);
		setContentView(R.layout.first);

		Button dataNascimento = (Button) findViewById(R.id.buttonDtNascimento);
		Date dataAtual = new Date();
		SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy");
		dataNascimento.setText(f.format(dataAtual));
		dataNascimento.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				Dialogs.showDialogDate(v);
			}

		});

		ImageView save = (ImageView) findViewById(R.id.imageViewSave);
		save.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				popularUsuario();
			}
		});

	}

	public void popularUsuario() {
		String nome = ((EditText) findViewById(R.id.editTextNome)).getText()
				.toString();
		String sexo = ((RadioButton) findViewById(R.id.radioF)).isChecked() ? "Feminino"
				: "Masculino";
		String dataNascimento = ((Button) findViewById(R.id.buttonDtNascimento))
				.getText().toString();
		String pesoStr = ((EditText) findViewById(R.id.editTextPeso)).getText()
				.toString();
		String alturaStr = ((EditText) findViewById(R.id.editTextAltura))
				.getText().toString();

		if (nome.trim().equals("")) {
			Toast.makeText(this, "Informe seu nome", Toast.LENGTH_SHORT).show();
			return;
		}

		if (sexo.trim().equals("")) {
			Toast.makeText(this, "Informe seu sexo", Toast.LENGTH_SHORT).show();
			return;
		}

		if (dataNascimento.trim().equals("")) {
			Toast.makeText(this, "Informe seu ano", Toast.LENGTH_SHORT).show();
			return;
		}

		if (pesoStr.trim().equals("")) {
			Toast.makeText(this, "Informe seu peso", Toast.LENGTH_SHORT).show();
			return;
		}

		if (alturaStr.trim().equals("")) {
			Toast.makeText(this, "Informe sua altura", Toast.LENGTH_SHORT).show();
			return;
		}

		UsuarioBean u = new UsuarioBean(nome, sexo, dataNascimento,
				Double.parseDouble(alturaStr), Double.parseDouble(pesoStr),
				1, 1);
		
		UsuarioData data = new UsuarioData(this);
		data.insert(u);
		
		LoadBeans.setUsuario(u);
		
		Intent intent = new Intent(First.this, Main.class);
		startActivity(intent);
	}

}
